<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Manipulate images using standard methods such as resize, crop, rotate, etc.
 * This class must be re-initialized for every image you wish to manipulate.
 *
 * $Id: Image.php 3917 2009-01-21 03:06:22Z zombor $
 *
 * @package    Image
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class Image extends Image_Core {

	public function colorize($color){
		$colorarray=(sscanf($color, '%2x%2x%2x'));
		
		$r=$colorarray[0]; 
		$g=$colorarray[1];
		$b=$colorarray[2];
		
		switch($this->ext){
			case'jpg':
			$img=imagecreatefromjpeg($this->file);
			break;
			case 'png':
			$img=imagecreatefrompng($this->file);
			break;
		}
		switch($this->ext){
			case'jpg':
			$this->image_colorize($img,$colorarray );	
			break;
			case 'png':
			$this->image_colorize_alpha($img,$colorarray );	
			break;
		}
		
		
		switch($this->ext){
			case'jpg':
			imagejpeg($img, $this->file,80);
			break;
			case 'png':
			imagepng($img, $this->file);
			break;
		}
	
	}
	
	public function image_colorize(&$img,$rgb) {
	
	  imageTrueColorToPalette($img,true,256);
	  $numColors = imageColorsTotal($img);
	
	  for ($x = 0; $x < $numColors; $x++) {
		
		list($r,$g,$b) = array_values(imageColorsForIndex($img,$x));
		
		$rbase=$rgb[0];
		$gbase=$rgb[1];
		$bbase=$rgb[2];
		
		// calculate grayscale in percent
		$grayscale = ($r + $g + $b) / 3.0 / 0xff;
		
		$rdiff=255-$rgb[0];
		$gdiff=255-$rgb[1];
		$bdiff=255-$rgb[2];	
		
		$rnew=$rgb[0]+$grayscale*$rdiff;
		$gnew=$rgb[1]+$grayscale*$gdiff;
		$bnew=$rgb[2]+$grayscale*$bdiff;
		
		imageColorSet($img,$x,$rnew, $gnew, $bnew );
	  //	imageColorSet($img,$x, $grayscale*255, $grayscale*255, $grayscale*255);
	  }
	
	  return true;
	}





	public function image_colorize_alpha(&$img,$rgb) {
	
		$width=imagesx($img);
		$height=imagesy($img);
		
		imagealphablending($img, false);
		imagesavealpha($img,true);
		
		for($x=0;$x<$width;$x++){
			for($y=0;$y<$height;$y++){
				
				$corig=$this->int2rgba(imagecolorat($img, $x, $y));
				
				$r=$corig['r'];
				$g=$corig['g'];
				$b=$corig['b'];
				$a=$corig['a'];
							
				$grayscale = ($r + $g + $b) / 3 / 0xff;
				
				$rdiff=255-$rgb[0];
				$gdiff=255-$rgb[1];
				$bdiff=255-$rgb[2];
				
				$rnew=$rgb[0]+$grayscale*$rdiff;
				$gnew=$rgb[1]+$grayscale*$gdiff;
				$bnew=$rgb[2]+$grayscale*$bdiff;		
				
				$cnew=imagecolorallocatealpha($img, $rnew,$gnew,$bnew,$a);
				imagesetpixel($img, $x, $y, $cnew); 
			}
		}
	  return true;
	}

	
	public function rgba2int($r, $g, $b, $a=1) { 
		return ($a << 24) + ($b << 16) + ($g << 8) + $r; 
	} 
	
	function int2rgba($int) { 
		$a = ($int >> 24) & 0xFF; 
		$r = ($int >> 16) & 0xFF; 
		$g = ($int >> 8) & 0xFF; 
		$b = $int & 0xFF; 
		return array('r'=>$r, 'g'=>$g, 'b'=>$b, 'a'=>$a); 
	} 

	public function add_dropshadow($sw=10, $sh=10){
		switch($this->ext){
			case'jpg':
			$img=imagecreatefromjpeg($this->file);
			break;
			case 'png':
			$img=imagecreatefrompng($this->file);
			break;
		}
		
		$dsbottom=imagecreatefrompng('mediaadmin/img_dropshadow/Bilder/dropshadow_orig_05.png');
		$dsright=imagecreatefrompng('mediaadmin/img_dropshadow/Bilder/dropshadow_orig_04.png');
		
		$imgw=imagesx($img);
		$imgh=imagesy($img);
				
		$newimg=imagecreatetruecolor($imgw+$sw, $imgh+$sh);		
		$col=imagecolorallocatealpha($newimg, 255,0,0,127);
		imagefill($newimg, 0,0,$col);
		imagealphablending($newimg, false);
		imagesavealpha($newimg,true);
	
		imagecopy($newimg,$img,0,0,0,0,$imgw, $imgh);
		imagecopyresampled($newimg, $dsbottom,0,$imgh,0,0,$imgw, $sh,imagesx($dsbottom),imagesy($dsbottom));
		imagecopyresampled($newimg,$dsright,$imgw,0,0,0,$sw,$imgh+$sh,imagesx($dsright),imagesy($dsright));
		
		imagepng($newimg, $this->file);
	
	}
	
	public function add_shineout($sw=10, $sh=10){
		switch($this->ext){
			case'jpg':
			$img=imagecreatefromjpeg($this->file);
			break;
			case 'png':
			$img=imagecreatefrompng($this->file);
			break;
		}
		
		$dstopleft=imagecreatefrompng('mediaadmin/img/dropshadow/Bilder/dropshadow_01.png');
		$dstopcenter=imagecreatefrompng('mediaadmin/img/dropshadow/Bilder/dropshadow_02.png');
		$dstopright=imagecreatefrompng('mediaadmin/img/dropshadow/Bilder/dropshadow_03.png');	
		$dsmiddleleft=imagecreatefrompng('mediaadmin/img/dropshadow/Bilder/dropshadow_04.png');
		$dsmiddleright=imagecreatefrompng('mediaadmin/img/dropshadow/Bilder/dropshadow_06.png');
		$dsbottomleft=imagecreatefrompng('mediaadmin/img/dropshadow/Bilder/dropshadow_07.png');
		$dsbottomcenter=imagecreatefrompng('mediaadmin/img/dropshadow/Bilder/dropshadow_08.png');
		$dsbottomright=imagecreatefrompng('mediaadmin/img/dropshadow/Bilder/dropshadow_09.png');	
		
		
		$imgw=imagesx($img);
		$imgh=imagesy($img);
				
		$newimg=imagecreatetruecolor($imgw+2*$sw, $imgh+2*$sh);		
		$col=imagecolorallocatealpha($newimg, 255,0,0,127);
		imagefill($newimg, 0,0,$col);
		imagealphablending($newimg, false);
		imagesavealpha($newimg,true);
		
		imagecopy($newimg,$img,$sw,$sh,0,0,$imgw, $imgh);
		imagecopyresampled($newimg, $dstopleft,0,0,0,0,$sw, $sh,imagesx($dstopleft),imagesy($dstopleft));
		imagecopyresampled($newimg, $dstopcenter,$sw,0,0,0,$imgw, $sh,imagesx($dstopcenter),imagesy($dstopcenter));
		imagecopyresampled($newimg, $dstopright,$sw+$imgw,0,0,0,$sw, $sh,imagesx($dstopright),imagesy($dstopright));
		imagecopyresampled($newimg, $dsmiddleleft,0,$sh,0,0,$sw, $imgh,imagesx($dsmiddleleft),imagesy($dsmiddleleft));
		imagecopyresampled($newimg, $dsmiddleright,$sw+$imgw,$sh,0,0,$sw, $imgh,imagesx($dsmiddleright),imagesy($dsmiddleright));
		imagecopyresampled($newimg, $dsbottomleft,0,$sh+$imgh,0,0,$sw, $sh,imagesx($dsbottomleft),imagesy($dsbottomleft));
		imagecopyresampled($newimg, $dsbottomcenter,$sw,$sh+$imgh,0,0,$imgw, $sh,imagesx($dsbottomcenter),imagesy($dsbottomcenter));
		imagecopyresampled($newimg, $dsbottomright,$sw+$imgw,$sh+$imgh,0,0,$sw, $sh,imagesx($dsbottomright),imagesy($dsbottomright));
		
		imagepng($newimg, $this->file);
	
	}


} // End Image